//
//  ZSMainTabbar.swift
//  01-ZSweibo
//
//  Created by shan on 15/12/16.
//  Copyright © 2015年 LZS. All rights reserved.
//

import UIKit

class ZSMainTabbar: UITabBar {

    // 5个子控件
    private let count: CGFloat = 5
    // 布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算每个UITabBarButton的宽度
        let width = self.bounds.width / count
        
        // 第一个UITabBarButton 的frame
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        
        // 遍历子控件
        var index: CGFloat = 0
        for view in self.subviews {
            
            // 剔除掉 composeButton 设置其他控件frame
            if view is UIControl && !(view is UIButton){
                
                view.frame = CGRectOffset(frame, index * width, 0)
                
                index += index == 1 ? 2 : 1
                
            }
            
        }
        // 设置加号按钮frame
        composeButton.frame = CGRectOffset(frame, 2.0 * width, 0)
    }
    
//    MARK: -   懒加载

    lazy var composeButton: UIButton = {
    
        let button = UIButton()
        
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加子控件
        self.addSubview(button)
        
        return button
    }()

}
